import { _decorator, Component, Node, EventTouch, Vec3, UITransform, Vec2, Slider, RigidBody } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('TouchMoveControls')
export class TouchMoveControls extends Component {

    @property(Node) fightMove01: Node
    @property(Node) fightMove02: Node
    @property(Node) player: Node
    @property(Node) upDownStream: Node

    fightMove01Pos: Vec3 = null
    fightMove01Radius: number = 0
    fightMove02Pos: Vec3 = null
    movementSpeed: number = 1
    moveFlag: boolean = false
    angle: number
    eulerAngles: Vec3 = new Vec3(0, 0, 0)
    resetEulerAngles: Vec3 = new Vec3(0, 0, 0)

    start() {
        this.fightMove01Pos = this.fightMove01.getWorldPosition()
        this.fightMove01Radius = this.fightMove01.getComponent(UITransform).contentSize.width / 2
        this.fightMove02Pos = this.fightMove02.getWorldPosition()
        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
    }

    update(deltaTime: number) {
        this.doMove()
    }

    onTouchStart(EventTouch: EventTouch) {
        // const pos = EventTouch.getLocationInView()
        // const height = this.node.parent.getComponent(UITransform).contentSize.height
        // this.fightMove01.setWorldPosition(pos.x, height - pos.y, 0)
    }

    onTouchMove(EventTouch: EventTouch) {
        const fightMove01Pos = this.fightMove01.getWorldPosition()
        const fightMove02Pos = this.fightMove02.getWorldPosition()
        // const pos = EventTouch.getLocationInView()
        // fightMove02Pos.x = pos.x
        // fightMove02Pos.y = 720 - pos.y

        const pos = EventTouch.getDelta()
        fightMove02Pos.x += pos.x
        fightMove02Pos.y += pos.y
        const offset = Vec2.distance(fightMove01Pos, fightMove02Pos)

        // 计算轮盘角度
        const offsetY = Math.abs(fightMove01Pos.y - fightMove02Pos.y)
        const offsetX = Math.abs(fightMove01Pos.x - fightMove02Pos.x)
        let angle = Math.atan(offsetY / offsetX) / (2 * Math.PI / 360)
        if (fightMove02Pos.x < fightMove01Pos.x && fightMove02Pos.y > fightMove01Pos.y) {
            angle = 180 - angle
        } else if (fightMove02Pos.x < fightMove01Pos.x && fightMove02Pos.y <= fightMove01Pos.y) {
            angle += 180
        } else if (fightMove02Pos.x >= fightMove01Pos.x && fightMove02Pos.y < fightMove01Pos.y) {
            angle = 360 - angle
        }

        angle %= 360
        angle = Boolean(angle) ? angle : 0
        // console.log("🚀 ~ file: TouchMoveControls.ts:45 ~ TouchMoveControls ~ onTouchMove ~ angle:", angle)

        // 两点距离超过半径 将其计算到半径内
        if (offset > this.fightMove01Radius) {
            let x = fightMove01Pos.x + Math.cos((2 * Math.PI / 360) * angle) * this.fightMove01Radius
            let y = fightMove01Pos.y + Math.sin((2 * Math.PI / 360) * angle) * this.fightMove01Radius
            fightMove02Pos.x = x
            fightMove02Pos.y = y
        }

        // 记录轴
        this.angle = angle

        // 角色移动
        this.moveFlag = true

        // 轮盘坐标改变
        this.fightMove02.setWorldPosition(fightMove02Pos)
    }

    onTouchEnd(EventTouch: EventTouch) {
        this.fightMove01.setWorldPosition(this.fightMove01Pos)
        this.fightMove02.setWorldPosition(this.fightMove02Pos)
        this.moveFlag = false
    }

    doMove() {
        // 保证在碰撞过程中重心稳住
        this.player.eulerAngles = this.resetEulerAngles

        // 角色上下游操作
        const slider = this.upDownStream.children[0].getComponent(Slider)
        const progress = slider.progress
        const xRoutate = (0.5 - progress) * 180
        this.eulerAngles.set(-xRoutate, this.eulerAngles.y, this.eulerAngles.z)
        this.player.children[0].eulerAngles = this.eulerAngles

        if (!this.moveFlag) {
            return
        }

        const playerPos = this.player.getPosition()
        const rotation = this.player.children[2].eulerAngles
        // 移动速度受体型影响
        this.movementSpeed = Math.sqrt(this.player.getScale().x)
        this.movementSpeed = Boolean(this.movementSpeed) ? this.movementSpeed : 1
        let moveLen = this.movementSpeed * 1

        // 最高地形20 最小地形1
        // 高度小于20 速度降低
        if (playerPos.y < 20) {
            moveLen *= (playerPos.y + 5) / 25
        } else {
            moveLen *= 1.5
        }

        //角色转轴
        this.eulerAngles.set(this.eulerAngles.x, this.angle - 90 + rotation.y, this.eulerAngles.z)
        this.player.children[0].eulerAngles = this.eulerAngles

        const moveX = moveLen * Math.cos((2 * Math.PI / 360) * (this.angle + rotation.y))
        const moveY = moveLen * Math.sin((2 * Math.PI / 360) * xRoutate)
        const moveZ = moveLen * Math.sin((2 * Math.PI / 360) * (this.angle + rotation.y))
        playerPos.add3f(moveX, -moveY, -moveZ)
        this.player.setPosition(playerPos)

    }


}


